from json import dump#,dumps
from flask import Blueprint,render_template,jsonify
from database import SeaOrganism,SeaTemperature,SeaChlorophyll,RedTideNews,RedTide,to_json
from datetime import datetime
#from sqlalchemy import or_

bp=Blueprint('monitor',__name__,url_prefix='/monitor')

@bp.route('/')
def monitor():
    return render_template('monitor.html')


@bp.route('/organism')
def get_SeaOrganism():
    year=datetime.now().year
    last_year_datas = SeaOrganism.query.filter(SeaOrganism.year == year - 1).limit(5)
    last_year_datas=list(last_year_datas)
    last_year_data_list = []
    for last_year_data in last_year_datas:
        #print(last_year_data.number)
        last_year_data_dict = {'num': last_year_data.number, 'location': last_year_data.location, 'year': last_year_data.year}
        last_year_data_list.append(last_year_data_dict)
    before_last_year_datas=list(SeaOrganism.query.filter(SeaOrganism.year == year - 2).limit(5))
    before_last_data_list = []
    for before_last_year_data in before_last_year_datas:
        #print(before_last_year_data.number)
        before_last_year_data_dict = {'num': before_last_year_data.number, 'location': before_last_year_data.location,
                               'year': before_last_year_data.year}
        before_last_data_list.append(before_last_year_data_dict)
    data_dict={'last_year_data':last_year_data_list,'before_year_data':before_last_data_list}#去年和前年
    # last_year_num=SeaOrganism.query.filter(SeaOrganism.year==year-1).with_entities(SeaOrganism.number).limit(5)
    # print(list(last_year_num))
    print(data_dict)
    # 如果不做转换，python会把字典的双引号转换成单引号，而单引号在前端页面中，js识别会出错。
    print(jsonify(data_dict))#dump(data_dict)

    with open('json/SeaOrganism.json', 'w', encoding="utf-8") as file:
        dump(data_dict,file,indent=2,ensure_ascii=False)  # 将数据写入到名为"data.json"的文件中 indent:缩进 ,ensure_ascii=False

    return jsonify(data_dict)

@bp.route('/temperature')
def update_SeaTemperature():
    temp_datas=list(SeaTemperature.query.order_by(SeaTemperature.time.desc()).limit(10))
    #temperature=123Sea.query.order_by(Sea.joint_time.desc()).with_entities(Sea.temperature).limit(5)
    temp_data_list = []
    for temp_data in reversed(temp_datas):
        # print(last_year_data.number)
        temp_data_dict = {'temperature': temp_data.temperature, 'time': temp_data.time}
        temp_data_list.append(temp_data_dict)
    print(temp_data_list)
    with open('json/SeaTemperature.json', 'w',encoding="utf-8") as file:
        dump(temp_data_list, file, indent=2,ensure_ascii=False)  # 将数据写入到名为"data.json"的文件中 indent:缩进

    return jsonify(temp_data_list)

@bp.route('/chlorophyll')
def update_chlorophyll():
    chlorophyll_datas=list(SeaChlorophyll.query.order_by(SeaChlorophyll.joint_time.desc()).limit(7))
    #return render_template('monitor.html')
    chlorophyll_data_list = []
    for chlorophyll_data in reversed(chlorophyll_datas):
        print(chlorophyll_data.joint_time)
        chlorophyll_data_dict = {'chlorophyll_a': chlorophyll_data.chlorophyll_a,'chlorophyll_b': chlorophyll_data.chlorophyll_b,
                                 'chlorophyll_c': chlorophyll_data.chlorophyll_c,'time': str(chlorophyll_data.joint_time)[5:]}
        chlorophyll_data_list.append(chlorophyll_data_dict)
    print(chlorophyll_data_list)
    with open('json/SeaChlorophyll.json', 'w', encoding="utf-8") as file:
        dump(chlorophyll_data_list, file, indent=2, ensure_ascii=False)  # 将数据写入到名为"data.json"的文件中 indent:缩进

    return jsonify(chlorophyll_data_list)

@bp.route('/RedTideNews')
def update_RedTideNews():
    red_tide_news=RedTideNews.query.order_by(RedTideNews.id.desc()).limit(1).first()#list(RedTideNews.query.order_by(RedTideNews.id.desc()).limit(1))[0]
    print(red_tide_news)
    red_tide_news_dict={'sea':red_tide_news.sea,'time':red_tide_news.time,'rank':red_tide_news.rank}
    print(red_tide_news_dict)
    return jsonify(red_tide_news_dict)

@bp.route('/RedTideDistribution')
def update_RedTideDistribution():
    year = datetime.now().year
    #red_tide_distributions=list(RedTide.query.filter(RedTide.province==None,or_(RedTide.year>2019,RedTide.year<=2017)))
    red_tide_distributions = list(RedTide.query.filter(RedTide.year==year))
    red_tide_distribution_list=[]
    for red_tide_distribution in red_tide_distributions:
        red_tide_distribution_dict={'frequency':red_tide_distribution.frequency,'province':red_tide_distribution.province}
        red_tide_distribution_list.append(red_tide_distribution_dict)
    print(red_tide_distribution_list)
    with open('json/RedTideDistribution.json', 'w', encoding="utf-8") as file:
        dump(red_tide_distribution_list, file, indent=2, ensure_ascii=False)  # 将数据写入到名为"data.json"的文件中 indent:缩进
    return jsonify(red_tide_distribution_list)

@bp.route('/RedTideDiscovery')
def update_RedTideDiscovery():
    year = datetime.now().year
    east_sea_datas = list(RedTide.query.filter(RedTide.sea=='东海',RedTide.year>=year-3,RedTide.year<=year-1))
    yellow_sea_datas = list(RedTide.query.filter(RedTide.sea == '黄海', RedTide.year >= year - 3, RedTide.year <= year - 1))
    bo_sea_datas = list(RedTide.query.filter(RedTide.sea == '渤海', RedTide.year >= year - 3, RedTide.year <= year - 1))
    south_sea_datas = list(RedTide.query.filter(RedTide.sea == '南海', RedTide.year >= year - 3, RedTide.year <= year - 1))

    east_sea_data_list,yellow_sea_data_list,bo_sea_data_list,south_sea_data_list=[],[],[],[]
    for east_sea_data,yellow_sea_data,bo_sea_data,south_sea_data in zip(east_sea_datas,yellow_sea_datas,bo_sea_datas,south_sea_datas):
        east_sea_data_dict = {'frequency': east_sea_data.frequency,'year':east_sea_data.year}
        east_sea_data_list.append(east_sea_data_dict)
        yellow_sea_data_dict = {'frequency': yellow_sea_data.frequency, 'year': yellow_sea_data.year}
        yellow_sea_data_list.append(yellow_sea_data_dict)
        bo_sea_data_dict = {'frequency': bo_sea_data.frequency, 'year': bo_sea_data.year}
        bo_sea_data_list.append(bo_sea_data_dict)
        south_sea_data_dict = {'frequency': south_sea_data.frequency, 'year': south_sea_data.year}
        south_sea_data_list.append(south_sea_data_dict)
    print(east_sea_data_list)
    sea_data_dict={'east_sea':east_sea_data_list,'yellow_sea':yellow_sea_data_list,'bo_sea':bo_sea_data_list,'south_sea':south_sea_data_list}
    with open('json/RedTideDiscovery.json', 'w', encoding="utf-8") as file:
        dump(sea_data_dict, file, indent=2, ensure_ascii=False)  # 将数据写入到名为"data.json"的文件中 indent:缩进
    return jsonify(sea_data_dict)

@bp.route('/RedTideArea')
def update_RedTideArea():
    year = datetime.now().year
    east_sea_datas = list(RedTide.query.filter(RedTide.sea=='东海',RedTide.year>=year-7,RedTide.year<=year-1))
    yellow_sea_datas = list(RedTide.query.filter(RedTide.sea == '黄海', RedTide.year >= year - 7, RedTide.year <= year - 1))
    bo_sea_datas = list(RedTide.query.filter(RedTide.sea == '渤海', RedTide.year >= year - 7, RedTide.year <= year - 1))
    south_sea_datas = list(RedTide.query.filter(RedTide.sea == '南海', RedTide.year >= year - 7, RedTide.year <= year - 1))

    east_sea_data_list,yellow_sea_data_list,bo_sea_data_list,south_sea_data_list=[],[],[],[]
    for east_sea_data,yellow_sea_data,bo_sea_data,south_sea_data in zip(east_sea_datas,yellow_sea_datas,bo_sea_datas,south_sea_datas):
        east_sea_data_dict = {'area': east_sea_data.area,'year':east_sea_data.year}
        east_sea_data_list.append(east_sea_data_dict)
        yellow_sea_data_dict = {'area': yellow_sea_data.area, 'year': yellow_sea_data.year}
        yellow_sea_data_list.append(yellow_sea_data_dict)
        bo_sea_data_dict = {'area': bo_sea_data.area, 'year': bo_sea_data.year}
        bo_sea_data_list.append(bo_sea_data_dict)
        south_sea_data_dict = {'area': south_sea_data.area, 'year': south_sea_data.year}
        south_sea_data_list.append(south_sea_data_dict)
    print(east_sea_data_list)
    sea_data_dict={'east_sea':east_sea_data_list,'yellow_sea':yellow_sea_data_list,'bo_sea':bo_sea_data_list,'south_sea':south_sea_data_list}
    with open('json/RedTideArea.json', 'w', encoding="utf-8") as file:
        dump(sea_data_dict, file, indent=2, ensure_ascii=False)  # 将数据写入到名为"data.json"的文件中 indent:缩进
    return jsonify(sea_data_dict)